package com.example.systemwork.entity;

import com.example.systemwork.common.entity.LogicEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

@Entity
@Getter
@Setter
@DiscriminatorValue("0")
public class Business extends User {
    @Column
    private String BusinessName;

    @OneToMany(mappedBy = "business",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"business"})
    private Set<Shop> shops;

    @OneToMany(mappedBy = "business",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"business"})
    private Set<Item> items;

    private Shop findShop(String shopName){
        AtomicReference<Shop> result= new AtomicReference<>(null);
        shops.forEach(shop->{
            if(shop.getShopName().equals(shopName)){
                result.set(shop);
            }
        });
        return result.get();
    }

    public Shop addShop(String shopName){
        Shop shop=findShop(shopName);
        if(shop==null){
            shop=new Shop();
            shop.setShopName(shopName);
            shop.setBusiness(this);
            shops.add(shop);
        }
        return shop;
    }
}

